﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImryStefanVRP
{
    public partial class Display : Form
    {
        CVRPSolution test = new CVRPSolution();
        CVRPData test2 = new CVRPData();
        public Display(CVRPSolution solution,CVRPData data)
        {
            InitializeComponent();
            test = solution;
            test2 = data;

        }
                
        //int Total = 100;
        //int Current = 100;


        Graphics g;
        Bitmap btm;
        Rectangle r;
        Pen p;

        Timer t = new Timer();
        Rectangle progressbar = new Rectangle(20, 20, 20, 20);
        Rectangle secondbar = new Rectangle(20, 200, 20, 20);
        Rectangle customer = new Rectangle();
        List<Rectangle> progressbarmatrix = new List<Rectangle>();
        List<StatusBar> statusbararra = new List<StatusBar>();
        List<Point> node= new List<Point>();
        List<int> X=new List<int>();
        List<int> Y=new List<int>();
        Graphics gr;

        StatusBar statusone;
        StatusBar stat;

        //StatusBar sh = new StatusBar();
        public class StatusBar
        {
            int Total = 100;
            int Current = 100;

            Graphics g;
            Bitmap btm;
            Rectangle r;
            Pen p;
            Timer t = new Timer();
            Color theme;

            public StatusBar(Rectangle Area, Color Theme)
            {
                p = new Pen(new SolidBrush(Theme));

                btm = new Bitmap(Area.Width, Area.Height);
                g = Graphics.FromImage(btm);
                r = new Rectangle(0, 0, Area.Width, Area.Height);

                t.Interval = 10;
                t.Tick += t_Tick;
                t.Start();
                theme = Theme;
            }

            public int pencent(double total, double current)
            {
                return (int)((current / total) * 100);
            }


            public void Drawbar()
            {
                SolidBrush color = new SolidBrush(theme);
                g.Clear(Color.White);
                g.FillRectangle(Brushes.White, r);
                g.DrawRectangle(new Pen(color), r);
                g.FillRectangle(color, new Rectangle(r.X, r.Y, (r.Width * (pencent(Total, Current))) / 100, r.Height));


            }
            void t_Tick(object sender, EventArgs e)
            {
             
                Drawbar();
            }

            public Bitmap GiveGraphic()
            {
                return btm;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            g = CreateGraphics();
            t.Interval = 10;
            t.Tick += t_Tick;
            norm2(test2);
            for (int i = 0; i < test2.Dimension; i++)
            {   
                customer = new Rectangle((int)test2.Coordinates[i,0],(int)test2.Coordinates[i,1],5,5);
                statusone = new StatusBar(customer, Color.DarkBlue);
                statusbararra.Add(statusone);
                progressbarmatrix.Add(customer);         
                node.Add(new Point((int)test2.Coordinates[i, 0],(int)test2.Coordinates[i, 1]));

            }
            
            t.Start();
        }

        void t_Tick(object sender, EventArgs e)
        {

            //g.DrawImage(statusone.GiveGraphic(), progressbar);
            //g.DrawImage(stat.GiveGraphic(), secondbar);
        }

        
        public void drawmethod()
        {
            Random random = new Random();
            int sizex = pictureBox1.Width;
            int sizey = pictureBox1.Height;
            btm = new Bitmap(sizex, sizey);
            g = Graphics.FromImage(btm);
            Label test1 = new Label();
            List<Color> allcolor=new List<Color>();
            for (int i = 0; i < test2.Dimension; i++)
            {
                g.DrawImage(statusbararra[i].GiveGraphic(), progressbarmatrix[i]);
            }

            for (int i = 0; i < 20; i++)
            {
                allcolor.Add(System.Windows.Forms.Label.DefaultBackColor);
            }
                for (int i = 0; i < test.listOfRoutes.Count; i++)
                {
                    Color route_color = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
                    allcolor[i] = route_color;
                    for (int j = 0; j < test.listOfRoutes[i].nodesArray.Count - 1; j++)
                    {
                        g.DrawLine(new Pen(route_color, 1), node[test.listOfRoutes[i].nodesArray[j]], node[test.listOfRoutes[i].nodesArray[j + 1]]);

                    }

                }

            label1.ForeColor = allcolor[0];
            label1.Text = "Route1";
            label1.Font = new Font("Georgia", 12);

            label2.ForeColor = allcolor[1];
            label2.Text = "Route2";
            label2.Font = new Font("Georgia", 12);

            label3.ForeColor = allcolor[2];
            label3.Text = "Route3";
            label3.Font = new Font("Georgia", 12);

            label4.ForeColor = allcolor[3];
            label4.Text = "Route4";
            label4.Font = new Font("Georgia", 12);

            label5.ForeColor = allcolor[4];
            label5.Text = "Route5";
            label5.Font = new Font("Georgia", 12);

            label6.ForeColor = allcolor[5];
            label6.Text = "Route6";
            label6.Font = new Font("Georgia", 12);

            label7.ForeColor = allcolor[6];
            label7.Text = "Route7";
            label7.Font = new Font("Georgia", 12);

            label8.ForeColor = allcolor[7];
            label8.Text = "Route8";
            label8.Font = new Font("Georgia", 12);

            label9.ForeColor = allcolor[8];
            label9.Text = "Route9";
            label9.Font = new Font("Georgia", 12);

            label10.ForeColor = allcolor[9];
            label10.Text = "Route10";
            label10.Font = new Font("Georgia", 12);

            label11.ForeColor = allcolor[10];
            label11.Text = "Route11";
            label11.Font = new Font("Georgia", 12);

            label12.ForeColor = allcolor[11];
            label12.Text = "Route12";
            label12.Font = new Font("Georgia", 12);

            label13.ForeColor = allcolor[12];
            label13.Text = "Route13";
            label13.Font = new Font("Georgia", 12);

            label14.ForeColor = allcolor[13];
            label14.Text = "Route14";
            label14.Font = new Font("Georgia", 12);

            label15.ForeColor = allcolor[14];
            label15.Text = "Route15";
            label15.Font = new Font("Georgia", 12);

            label16.ForeColor = allcolor[15];
            label16.Text = "Route16";
            label16.Font = new Font("Georgia", 12);

            label17.ForeColor = allcolor[16];
            label17.Text = "Route17";
            label17.Font = new Font("Georgia", 12);

            label18.ForeColor = allcolor[17];
            label18.Text = "Route18";
            label18.Font = new Font("Georgia", 12);

            label19.ForeColor = allcolor[18];
            label19.Text = "Route19";
            label19.Font = new Font("Georgia", 12);

            label20.ForeColor = allcolor[19];
            label20.Text = "Route20";
            label20.Font = new Font("Georgia", 12);

        }


        public CVRPData norm(CVRPData data)//this function normalize the coordinate
        {
            CVRPData testnew = new CVRPData(data);
            double minx = X.Min();
            double miny = Y.Min();
            double depotx = testnew.Coordinates[0, 0];
            double depoty = testnew.Coordinates[0, 1];
            double box_height = pictureBox1.Height;
            double box_width = pictureBox1.Width;
            for (int i = 0; i < data.Dimension; i++)
            {
                testnew.Coordinates[i, 0] = testnew.Coordinates[i, 0] - minx;
                testnew.Coordinates[i, 1] = testnew.Coordinates[i, 1] - miny;
            }
            return testnew;
        }

        public void norm2(CVRPData data)//this function normalize the coordinate
        {
            CVRPData testnew = new CVRPData();
            testnew = data;
            double box_height = pictureBox1.Height;
            double box_width = pictureBox1.Width;
            for (int i = 0; i < testnew.Dimension; i++)
            {
                customer = new Rectangle((int)testnew.Coordinates[i, 0], (int)testnew.Coordinates[i, 1], 5, 5);
                statusone = new StatusBar(customer, Color.DarkBlue);
                //statusbararra.Add(statusone);
                //progressbarmatrix.Add(customer);
                //node.Add(new Point((int)test2.Coordinates[i, 0], (int)test2.Coordinates[i, 1]));
                X.Add((int)testnew.Coordinates[i, 0]);
                Y.Add((int)testnew.Coordinates[i, 1]);

            }

            double minx = X.Min();
            double miny = Y.Min();
            double maxx = X.Max();
            double maxy = Y.Max();

            double depotx = testnew.Coordinates[0, 0];
            double depoty = testnew.Coordinates[0, 1];

            for (int i = 0; i < data.Dimension; i++)
            {
                testnew.Coordinates[i, 0] = (testnew.Coordinates[i, 0] - minx) / (maxx - minx + 20) * box_width;

                testnew.Coordinates[i, 1] = (testnew.Coordinates[i, 1] - miny) / (maxy - miny + 20) * box_height;

            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //for (int i = 0; i < test2.Dimension; i++)
            //{
            //    customer = new Rectangle((int)test2.Coordinates[i, 0], (int)test2.Coordinates[i, 1], 5, 5);
            //    statusone = new StatusBar(customer, Color.DarkBlue);
            //    statusbararra.Add(statusone);
            //    progressbarmatrix.Add(customer);
            //    node.Add(new Point((int)test2.Coordinates[i, 0], (int)test2.Coordinates[i, 1]));
            //    X.Add((int)test2.Coordinates[i, 0]);
            //    Y.Add((int)test2.Coordinates[i, 1]);

            //}

            drawmethod();
            //pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;           
            //pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = btm;

        }






    }
}
